/* global Cesium */
import TargetStyle from './TargetStyle';
import check from '../Core/check';
import primitiveType from './primitiveType';

const Cartesian3 = Cesium.Cartesian3;
const Color = Cesium.Color;
const CesiumMath = Cesium.Math;
const HeadingPitchRoll = Cesium.HeadingPitchRoll;
const Transforms = Cesium.Transforms;

const funcMap = {
    _point: '_getPoint',
    _billboard: '_getBillboard',
    _label: '_getLabel',
    _model: '_getModel',
    _mark: '_getMark',
};

/**
 * TargetItem 构造函数
 * @param id 目标对象的id
 * @param lon 目标对象的经度
 * @param lat 目标对象的纬度
 * @param alt 目标对象的高度
 * @param smoothTrack 是否需要平滑
 *  let item = new GV.TargetItem('gb324244234sdf432',113.654,36.542.6921,true)
 */
class TargetItem {
    constructor(id, lon, lat, alt, smoothTrack, showTrackLine) {
        if (smoothTrack === undefined) {
            smoothTrack = false;
        }
        if (showTrackLine === undefined) {
            showTrackLine = false;
        }
        this._pending = true;
        this._style = TargetStyle.DefaultStyle();
        this._angle = 0;
        this._parentLayer = null;
        this._init = false;
        this._type = null;
        this._smoothTrack = false;
        this._inView = false;
        this._timeArray = [];
        this._show = true;
        this._isSelected = false;
        this._labelInit = false;
        this._modelInit = false;
        this._id = id;
        this._name = '未命名';
        this._smoothTrack = smoothTrack;
        this._position = Cartesian3.fromDegrees(lon, lat, alt);
    }
    /**
     * 目标的类型编码
     * @method styleCode
     *
     * ```
     * 示例代码
     * let itemTypeCode = targetItem.styleCode ;
     * ```
     */
    get styleCode() {
        return this._type;
    }
    set styleCode(val) {
        this._type = val;
    }
    /**
     * 目标是否显示
     * @method Boolean
     */
    get show() {
        return this._show;
    }
    set show(val) {
        this._show = val;
        this._model.show = val;
        this._label.show = val;
        this._point.show = val;
        this._billboard.show = val;
        this._mark.show = val;
    }
    /**
     * 目标的样式
     * @method style
     *
     * ```
     * 示例代码
     * let style = targetItem.style;
     * ```
     */
    get style() {
        return this._style;
    }
    set style(val) {
        this._style = val;
        if (this._parentLayer) this._parentLayer.setTargetStyle(this);
    }
    /**
     * @method updatePosition
     * @param lon 经度
     * @param lat 纬度
     * @param alt 高度
     * ```
     * 示例代码
     * targetItem.updatePosition(113.254,36.254,10000);
     * ```
     */
    updatePosition(lon, lat, alt) {
        let position = Cartesian3.fromDegrees(Number(lon), Number(lat), Number(alt));
        position.clone(this._position);
        primitiveType.forEach(type => {
            this[type].position = this._position;
            this._updatePrimitive(type);
        });
        this._mark.position = this._position;
        if (
            this._parentLayer._markShow &&
            this._parentLayer.markTargetId.includes(this._id) &&
            !this._markInit
        ) {
            this._mark = this._parentLayer._markCollection.add(this._mark);
            this._markInit = true;
        } else if (
            (!this._parentLayer._markShow || !this._parentLayer.markTargetId.includes(this._id)) &&
            this._markInit
        ) {
            this._parentLayer._markCollection.remove(this._mark);
            this._mark = this._parentLayer._getMark(this);
            this._markInit = false;
        }
    }
    _updatePrimitive(type) {
        if (this._parentLayer[type + 'Show'] && !this[type + 'Init']) {
            this[type] = this._parentLayer[type + 'Collection'].add(this[type]);
            this[type + 'Init'] = true;
        } else if (!this._parentLayer[type + 'Show'] && this[type + 'Init']) {
            this._parentLayer[type + 'Collection'].remove(this[type]);
            this[type] = this._parentLayer[funcMap[type]](this);
            this[type + 'Init'] = false;
        }
    }
    setColor(pColor) {
        let color = Color.fromCssColorString(pColor);
        this._style.color = pColor;
        let point = this._point;
        let label = this._label;
        let billboard = this._billboard;
        point.color = color;
        billboard.color = color;
        if (label) label.color = color;
        if (this._model && this._model.model) this._model.model.color = color;
    }
    /**
     * 目标的名称
     * @method name
     *
     * ```
     * 示例代码
     * targetItem.name="RED";
     * ```
     */
    get name() {
        return this._name;
    }
    set name(val) {
        this._name = val;
        if (this._label) {
            this._label.text = val;
        }
    }
    /**
     * 目标颜色,css颜色值
     * @method color
     *
     * ```
     * 示例代码
     * targetItem.color="RED";
     * targetItem.color="#FF0000";
     * ```
     */
    get color() {
        return this._style.color;
    }
    set color(val) {
        this._style.color = val;
        this.setColor(val);
    }
    /**
     * 目标大小
     * @method iconSize
     */
    get iconSize() {
        return this._style.iconSize;
    }
    set iconSize(val) {
        this._style.iconSize = val;
        if (this._billboard) {
            this._billboard.width = val;
            this._billboard.height = val;
        }
    }
    /**
     * 目标大小
     * @method iconSize
     *
     * ```
     * 示例代码
     * targetItem.iconSize=24;
     * ```
     */
    get fontBackgroundColor() {
        return this._style.fontBackgroundColor;
    }
    set fontBackgroundColor(val) {
        this._style.fontBackgroundColor = val;
        if (this._label && this._labelInit) {
            this._label.backgroundColor = Color.fromCssColorString(val);
        }
    }
    /**
     * 目标是否选中
     * @method isSelected
     *
     * ```
     * 示例代码
     * targetItem.isSelected = true;
     * ```
     */
    get isSelected() {
        return this._isSelected;
    }
    set isSelected(val) {
        this._isSelected = val;
        if (val) {
            this.setColor(this._style.selectedColor);
        } else {
            this.setColor(this._style.color);
        }
    }
    /**
     * 目标角度
     * @method angle
     *
     * ```
     * 示例代码
     * targetItem.angle=105;
     * ```
     */
    get angle() {
        return this._angle;
    }
    set angle(val) {
        if (!check.isNumber(val)) return;
        this._angle = val;
        this._billboard && (this._billboard.rotation = -1 * CesiumMath.toRadians(this._angle));
        if (this._model) {
            let heading = CesiumMath.toRadians(val - 90);
            let hpr = new HeadingPitchRoll(heading, 0, 0);
            let orientation = Transforms.headingPitchRollQuaternion(
                this._model.position._value,
                hpr
            );
            this._model.orientation = orientation;
        }
    }
}

export default TargetItem;
